package info.touret.winecellar.reminder;

import info.touret.winecellar.bottle.server.domain.Bottle;
import info.touret.winecellar.dao.server.DAOModule;
import info.touret.winecellar.userinfo.server.domain.UserInfo;
import info.touret.winecellar.userinfo.shared.UserException;

import java.io.IOException;

import javax.inject.Inject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.google.appengine.api.users.User;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;

public class ReminderQueueServlet extends HttpServlet {

	public enum PARAMETERS {
		USERID, BOTTLEID;
	}

	public final String DATE_FORMAT = "yyyyMMdd";

	private static final long serialVersionUID = 4883282762092814151L;

	public ReminderQueueServlet() {
		super();

	}

	@Inject
	private ReminderService reminderservice;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest
	 * , javax.servlet.http.HttpServletResponse)
	 */
	@Override
	public void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		super.service(request, response);
		try {
			/* extraction des parametres envoyes a la requette HTTP */
			Long userid = Long.parseLong(request.getParameter(PARAMETERS.USERID
					.toString()));
			Long bottleid = Long.parseLong(request
					.getParameter(PARAMETERS.BOTTLEID.toString()));
			/* Recuperation de l utilisateur concerne */
			UserInfo userinfo = UserInfo.findUserInfo(userid);
			trace.error("user a trouver:" + userid + "  " + userinfo);

			if (userinfo == null) {
				// @todo modifier quand il y aura une console d administration /
				// voir import donnees
				UserInfo mockinfo = new UserInfo();
				mockinfo.setEmailAddress("alexandre@touret.info");
				mockinfo.setUserID(1L);
				mockinfo.setId(1L);
				userinfo = mockinfo;
				// throw new UserException("L utilisateur ("+user+
				// ") n existe pas en base");
			}

			ReminderModule module = new ReminderModule();
			module.setUserinfo(userinfo);
			Injector INJECTOR = Guice.createInjector(module);
			reminderservice = new ReminderServiceImpl();
			INJECTOR.injectMembers(reminderservice);
			Bottle bottle = Bottle.findBottle(bottleid);
			trace.info("Creation d un evenement pour l utilisateur :"
					+ userinfo);
			/* Creation de l evenement */
			reminderservice.createEvent(bottle, userinfo);
			trace.info("Evenement cree pour l utilisateur :" + userinfo);
			response.getWriter().println("event successfully added");
		} catch (NumberFormatException e) {
			trace.error(e.getMessage(), e);
			throw new ReminderException(e);
		} catch (ReminderException e) {
			trace.error(e.getMessage(), e);
			throw e;
		} catch (Exception e) {
			trace.error(e.getMessage(), e);
			throw new ReminderException(e);
		}
	}

	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		super.doPost(req, resp);
	}

	private static Logger trace = Logger.getLogger(ReminderQueueServlet.class);
}
